#include <string>
#include <iostream>
#include <time.h>
#include <stdlib.h>
#include <cstdio>
#include <vector>

#include "newuser.h"
#include "query.h"
#include "util.h"

using namespace std;

void NewUserLogin(string Email)
{

///Request the needed information to setup a profile for a new user
string Fname,Lname;
cout<<"User:"<<Email<<"'s First Name:";
cin>>Fname;
cout<<"User:"<<Email<<"'s Last Name:";
cin>>Lname;
///Add user to the DB
RegisterNewUser(Fname,Lname,Email);
}

void RegisterNewUser(string Fname,string Lname, string Email)
{
int RandomNumber=0;
bool NumberAvailable=true;
string RandomNumberString;
//generate unique ID for the user not available in the DB
do{
        srand(time(NULL));
        RandomNumber+=rand();
	///make sure generated number is positive so I wouldn't get -1 and -27 which are reserved
	if(RandomNumber<0)
		RandomNumber*=-1;

	///Setup query to check if ID already available
        string CheckIfRandomNumberUsedQuery="select exists(select 1 from Users where ID=";
        RandomNumberString=ConvertIntToString(RandomNumber);
        CheckIfRandomNumberUsedQuery+=RandomNumberString;
        CheckIfRandomNumberUsedQuery+=")";
	Query CheckIfRandomNumberUsed(CheckIfRandomNumberUsedQuery);
	CheckIfRandomNumberUsed.Execute();
        string RandomNumberUsed=CheckIfRandomNumberUsed.GetLastElementReturned();

        if(RandomNumberUsed=="1")///already used
                NumberAvailable=true;
        else///new ID
                NumberAvailable=false;
}
while(NumberAvailable==true);//Check if ID already used

///Get current time
char TimeDate[20];
time_t t=time(NULL);
strftime(TimeDate,20,"%Y-%m-%d %H:%M:%S",localtime(&t));

///Create query to add the new user
string AddNewUserQuery="INSERT INTO Users VALUES (";
AddNewUserQuery+=RandomNumberString;
AddNewUserQuery+=",'";
AddNewUserQuery+=Fname;
AddNewUserQuery+="','";
AddNewUserQuery+=Lname;
AddNewUserQuery+="','";
AddNewUserQuery+=Email;
AddNewUserQuery+="','";
AddNewUserQuery+=TimeDate;
AddNewUserQuery+="','";
AddNewUserQuery+=TimeDate;
AddNewUserQuery+="')";

//Execute query
Query AddNewUser(AddNewUserQuery);
AddNewUser.Execute();

//TODO: get all his assigned projects and add them to ProjectUsers;

///Retrieve all the projects that were already assigned to his email if they are available
string GetNewUserProjectsQuery="SELECT ProjectID FROM NewUsers WHERE Email='";
GetNewUserProjectsQuery+=Email;
GetNewUserProjectsQuery+="'";
Query GetNewUserProjects(GetNewUserProjectsQuery);
GetNewUserProjects.Execute();

///Check the number of preassigned projects he has
int NewUserProjectsCount=GetNewUserProjects.GetReturnElementsCount();

//int preassigned_project_count=SQL_query_multiple(retrieve_his_projects,preassigned_member_projects);//NOTE: CREATE A SPECIAL QUERY FUNCTION THAT WOULD RETURN array int with project numbers
vector<string> NewUserProjects=GetNewUserProjects.GetReturnElements();

///Add each preassigned project to match his new ID
for(int temp=0;temp<=NewUserProjectsCount;temp++)
	{
		string AddProjectToNewUserQuery="INSERT INTO ProjectUsers VALUES(";
		AddProjectToNewUserQuery+=NewUserProjects.at(temp);
		AddProjectToNewUserQuery+=",";
		AddProjectToNewUserQuery+=RandomNumberString;
		AddProjectToNewUserQuery+=");";
		//SQL_query(add_his_project);
		Query AddProjectToNewUser(AddProjectToNewUserQuery);
		AddProjectToNewUser.Execute();
	}

///Delete his entry fro new user Table
string DeleteTempDBNewUserProjectsQuery="DELETE FROM NewUsers WHERE Email='";
DeleteTempDBNewUserProjectsQuery+=Email;
DeleteTempDBNewUserProjectsQuery+="'";
//SQL_query(delete_temp_entry);
Query DeleteTempDBNewUserProjects(DeleteTempDBNewUserProjectsQuery);
DeleteTempDBNewUserProjects.Execute();
}


//NOTE TO SELF: TO DO NEXT TIME: get the date from the logwindow and retrieve the data for the date specified. Even if today. Setup the log file and export the data to it. Do the same for the command prompt interface
//NEXT modify the setupProject, missing date created and missing directory.
